#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-10-13 4:48 下午
# @Author  : yangdy
# @File    : 2020-10-13.24.两两交换链表中的节点
# @Software: PyCharmCE
from tools.my_methods import *


# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head
        p, q = head, head.next
        p.next = q.next
        q.next = p
        p.next = self.swapPairs(p.next)
        return q
